package com.example.sample.activity;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.R.id;
import com.example.sample.R.layout;

import net.liteheaven.xblib.common.ui.ToastUtil;

public class RegularExpressionActivity extends BaseActivity {

    private EditText mEtInput;
    private TextView mTvExpression;
    private TextView mTvResult;
    private Button mBtnOk;
    private ListView listView;
    private static final String EXP = "^\\s*[0-9]+:\\s+[A-Za-z0-9]+:([A-Za-z0-9]{4})\\s+[A-Za-z0-9]+:[A-Za-z0-9]{4}\\s+[A-Za-z0-9]{2}\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]+\\s+([0-9]+)\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[A-Za-z0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+-*[0-9]+\\s*";
    //private static final String EXP = "\\s+[0-9]+:\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]{2}\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]+:[A-Za-z0-9]+\\s+[A-Za-z0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[A-Za-z0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+[0-9]+\\s+";
    //private static final String EXP = "lan-[0-9]*$";
    //private static final String EXP = "java.*";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regular_expression);
        mEtInput = (EditText)findViewById(R.id.etInput);
        mEtInput.setText("   0: 0100007F:414A 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10001        0 5169 1 eb270500 300 0 0 2 -1");
        //mEtInput.setText("lan-3333333333333");
        mTvExpression = (TextView)findViewById(R.id.tvExpression);
        mTvResult = (TextView)findViewById(R.id.tvResult);
        mBtnOk = (Button)findViewById(R.id.btnOk);
        OnClickListener l = new OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = mEtInput.getText().toString();
                txt = txt.toLowerCase();
                mTvResult.setText("" + Pattern.matches(EXP, txt) + " " + txt.matches(EXP));
            }
        };
        mBtnOk.setOnClickListener(l);
        mTvExpression.setText(EXP);

        listView = findViewById(id.listView);
        listView.setAdapter(new ExpAdapter());
        TextView headerView = new TextView(this);
        headerView.setTextSize(30);
        headerView.setText("我是header");
        listView.addHeaderView(headerView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                long p = parent.getItemIdAtPosition(position);
                Bean bean = (Bean) parent.getItemAtPosition(position);
                if (bean == null){
                    ToastUtil.show(parent.getContext(), "别瞎JB点");
                }else{
                    Pattern pattern = Pattern.compile(bean.exp);
                    boolean match = pattern.matcher(bean.string).matches();
                    ToastUtil.show(parent.getContext(), match ? "匹配" : "不匹配");
                }
            }
        });
    }

    private static class Bean{
        public String string;
        public String exp;
        public Bean(String string, String exp){
            this.string = string;
            this.exp = exp;
        }
    }

    private static class ExpAdapter extends BaseAdapter{

        private List<Bean> list = new ArrayList<>();

        private ExpAdapter(){
            addPair("!123!", "[!]+123[!]+");
            addPair("123", "^123$");
            addPair("15002092015", "^[1][3,4,5,8][0-9]{9}$");
            addPair("1,002092015", "^[1][3,4,5,8][0-9]{9}$");
            addPair("15002092015", "^[1][3458][0-9]{9}$");
        }

        private void addPair(String string, String exp){
            list.add(new Bean(string, exp));
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position - 1;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (convertView == null){
                tv = new TextView(parent.getContext());
                tv.setTextSize(20);
            }else{
                tv = (TextView) convertView;
            }
            Bean bean = (Bean)getItem(position);
            tv.setText(bean.exp + " -> " + bean.string);
            return tv;
        }
    }
}
